package cn.fengliu.controller.Tea;

import cn.fengliu.Dao.MarkDao;
import cn.fengliu.Dao.TaskDao;
import cn.fengliu.Dao.UserDao;
import cn.fengliu.entity.Clz;
import cn.fengliu.entity.Mark;
import cn.fengliu.entity.Task;
import cn.fengliu.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller   //创建对象，并且装配到 IOC容器
@RequestMapping("/t_tea")
public class T_TeaController {
   @Autowired
   UserDao userDao;
@Autowired
    TaskDao taskDao;
@Autowired
    MarkDao markDao;
    public T_TeaController() {
        System.out.println("stuController 对象被创建了");
    }
    @RequestMapping("/findMyteask")
    public String findMyteask(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        if (user!=null) {
            System.out.println(user.getUid());
            List<Task> tasks = taskDao.findMyteask(user.getUid());
            System.out.println(tasks);
            model.addAttribute("tasks", tasks);
        }
        return "/tea/findMyteask";
}


///t_tea/findMark/'+${x.user.clz.clzno}+'/'+${x.course.cno}
    @RequestMapping("/findMark/{clzno}/{cno}")
    public String findMyMark(HttpSession session,Model model,
                                   @PathVariable("clzno") String clzno,
                                   @PathVariable("cno") String cno){
        System.out.println(clzno+cno);
        List<Mark> stuByclz = markDao.findStuByclz(clzno, cno);
        for (int i = 0; i <stuByclz.size();i++) {
            if (stuByclz.get(i).getId()==null){
                stuByclz.get(i).setId(0);
                System.out.println(stuByclz.get(i).getId());
            }
            if (stuByclz.get(i).getScore()==null){
                stuByclz.get(i).setScore(0.0);
                System.out.println(stuByclz.get(i).getScore());
            }
        }
        System.out.println(stuByclz);
       model.addAttribute("scores",stuByclz) ;
        return "/tea/findMyMark";

    }
    @RequestMapping("/action")
    public String action(Model model,Mark mark){
            mark.setTpost(new Date());
            System.out.println(mark);
            if (mark.getScore()<200&&mark.getScore()>0){
            if (mark.getId() == 0) {
                markDao.add(mark);
            } else {
                markDao.update(mark);
            }}
        model.addAttribute("info","分数异常");
        return "forward:/t_tea/findMark/"+mark.getUser().getClz().getClzno()+"/"+mark.getCourse().getCno();

    }

}
